﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

/**
 * 创建时间：2014/11/13 14:17:54
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace CUWo.Common.Converter
{
    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return string.Empty;

            try
            {
                if (value.ToString().Length == "yyyymmddhhmmss".Length)
                {   
                    string datetimeValue = value.ToString();
                    return string.Concat(datetimeValue.Substring(0, 4), "-", datetimeValue.Substring(4, 2), "-", datetimeValue.Substring(6, 2), " ", datetimeValue.Substring(8, 2), ":", datetimeValue.Substring(10, 2), ":", datetimeValue.Substring(12, 2));                    
                }
                else
                {
                    DateTime dt = DateTime.Parse(value.ToString());
                    if (null == parameter)
                    {
                        return dt.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(parameter.ToString().Trim()))
                            return dt.ToString("yyyy-MM-dd");
                        else
                            return dt.ToString(parameter.ToString().Trim());
                    }
                }
            }
            catch
            {
                return string.Empty;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
